#include "Sprite.h"
#include <iostream>


Sprite::Sprite(ID2D1Bitmap* tex,int s_width,int s_height){

	 texture = tex;
   	 
	 //Texture Size
	 D2D1_SIZE_F size = tex->GetSize();

	 wFrames = (int) size.width / s_width;
	 hFrames = (int) size.height / s_height;
	 currentFrame = 0;
	 lastFrame = wFrames * hFrames;

	 //Size per each Frame
	 sWidth = s_width;
	 sHeight = s_height;

	 alpha = 1.0f;

	 isAnimated = false;
	 isVisible	= true;
	 isLoop		= true;
}
Sprite::~Sprite(){
	
}
void Sprite::Render(D2D1_SIZE_F position, ID2D1HwndRenderTarget *render , FLOAT angle, D2D1_POINT_2F center, ID2D1SolidColorBrush *pBrush ){
	
	if (isVisible){
		int posX = (currentFrame % wFrames) * sWidth;
		int posY = (currentFrame / wFrames) * sHeight;

		//Updating BoundingBox
		boundingBox.left   = (float) position.width;
		boundingBox.top    = (float) position.height;
		boundingBox.right  = (float) position.width+sWidth;
		boundingBox.bottom = (float) position.height+sHeight;

		//Destination Box
		D2D1_RECT_F dstBox;
		dstBox.left   = (float) posX;
		dstBox.top    = (float) posY;
		dstBox.right  = (float) posX+sWidth;
		dstBox.bottom = (float) posY+sHeight;

		render->SetTransform(D2D1::Matrix3x2F::Translation(0.0F,0.0F));
		render->SetTransform(D2D1::Matrix3x2F::Rotation(angle,center));
		//render->SetTransform(D2D1::Matrix3x2F::Translation(dstBox.left,dstBox.top));
		// DrawBitmap(imagen, rectangulo donde vas a dibujar la imagen, alpha, interpolation mode, parte de la imagen que deseas dibujar(como un mask)
		render->DrawBitmap(texture ,boundingBox,alpha,D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,dstBox);
		render->SetTransform(D2D1::IdentityMatrix());

		if ( isAnimated )
			currentFrame++;

		if (isLoop && currentFrame > lastFrame ) {
			currentFrame = 0;
		}
	}

}

void Sprite::Render(D2D1_MATRIX_3X2_F matrix, ID2D1HwndRenderTarget *render, ID2D1SolidColorBrush *pBrush )
{
	if (isVisible){
		int posX = (currentFrame % wFrames) * sWidth;
		int posY = (currentFrame / wFrames) * sHeight;

		boundingBox.left   = (float) 0;
		boundingBox.top    = (float) 0;
		boundingBox.right  = (float) sWidth;
		boundingBox.bottom = (float) sHeight;
		//Destination Box
		D2D1_RECT_F dstBox;
		dstBox.left   = (float) posX;
		dstBox.top    = (float) posY;
		dstBox.right  = (float) posX+sWidth;
		dstBox.bottom = (float) posY+sHeight;
		render->SetTransform(matrix);
		//render->SetTransform(D2D1::Matrix3x2F::Translation(dstBox.left,dstBox.top));
		// DrawBitmap(imagen, rectangulo donde vas a dibujar la imagen, alpha, interpolation mode, parte de la imagen que deseas dibujar(como un mask)
		render->DrawBitmap(texture ,boundingBox,alpha,D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,dstBox);
		render->SetTransform(D2D1::IdentityMatrix());
		if ( isAnimated )
			currentFrame++;
	}
}